<template>
	<view class="business">
		<view class="navList">
			<view class="navitem" @click="toPage('list/list?id='+ item.id)" v-for="(item,index) in list.shop_class"
				:key="item.id">
				<image :src="item.img_url | getImg" mode=""></image>
				<text>{{item.name}}</text>
			</view>


		</view>
		<view class="businessbox">
			<view class="address">
				当前位置：{{address}}
			</view>
			<view class="businesslist">
				<view class="b_item" @click="toPage('detail/detail?id=' + item.id)" v-for="(item,index) in shop_list"
					:key="item.id">
					<image :src="item.img | getImg" mode=""></image>
					<view class="infobox">
						<view class="name">
							{{item.shop_name}}
						</view>
						<view class="gap">
							{{item.distance}}
						</view>
						<view class="address_name">
							{{item.shop_address}}
						</view>
					</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import {
		ShopList,
		ShopData
	} from "@/api/api.js"
	import {
		getShopArea
	} from "@/api/user.js"
	export default {
		data() {
			return {
				list: [],
				shop_list: [],
				latitude: '',
				longitude: '',
				address: '',
				page: 1,
				toggle: true
			}
		},
		onLoad() {
			this.ShopList()
			this.ShopData()
		},
		onShow() {
			this.getShopArea()
		},
		onReachBottom() {
			if (this.toggle) {
				this.page++
				this.ShopData()
			}
		},
		methods: {
			getShopArea() {

				uni.getLocation({
					altitude: false,
					geocode: true,
					isHighAccuracy: true,
					highAccuracyExpireTime: 300,
					success: (data) => {
						console.log(data);
						uni.getStorage({
							key: 'nowAddress',
							success: (res) => {
								let a = Number(data.latitude) - Number(res.data.latitude)
								if (a < 0.0001 && a > -0.0001) {
									this.address = res.data.address
									return
								} else {
									this.getShopAreas(data)
								}
							},
							fail: (err) => {
								this.getShopAreas(data)
							}
						})
					}
				})

			},
			getShopAreas(data) {
				getShopArea({
					latitude: data.latitude,
					longitude: data.longitude
				}).then(res => {
					this.address = res.data.address
					uni.setStorage({
						key: 'nowAddress',
						data: {
							address: this.address,
							latitude: data.latitude,
							longitude: data.longitude
						}
					})
				})
			},
			ShopList() {
				ShopList().then(res => {
					this.list = res.data
				})
			},
			ShopData() {
				ShopData({
					page: this.page
				}).then(res => {
					if (res.data.length > 0) {
						this.shop_list = this.shop_list.concat(res.data)
					} else {
						this.toggle = false
					}
				})
			},
			toPage(url) {
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f3f3f3;
	}

	.businessbox {
		background: #fff;
		margin-top: 10px;
		padding: 10px;

		.address {
			font-size: 14px;
			padding-bottom: 10px;
			border-bottom: 5px solid #f3f3f3;
		}

		.b_item {
			display: flex;
			align-items: center;
			padding: 10px 0;

			image {
				height: 80px;
				width: 80px;
			}

			.infobox {
				margin-left: 10px;
				flex: 1;

				.gap,
				.address_name {
					font-size: 14px;
					color: #777;
					margin-top: 5px;
				}
			}
		}
	}

	.navList {
		display: table;
		background-color: #fff;
		width: 100%;

		.navitem {
			float: left;
			width: 20%;
			text-align: center;
			padding: 10px 5px;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 14px;

			image {
				width: 50px;
				height: 50px;
				border-radius: 50%;
			}
		}

		.active {
			color: #FF5555;
			border-bottom: 2px solid #FF5555;
		}
	}
</style>
